/**
 * File:bc.tag
 * Youth is just a section of unoptimized code!
 * -------------------------------------------------------------------------
 * Created by BigQ on 2016/12/20.
 *--------------------------------------------------------------------------
 * This technical information is confidential information,
 * without my written permission is prohibited for commercial purposes.　
 *--------------------------------------------------------------------------
 */
(function ($) {
    var bcTag = function (ele, options) {
        this.element = $(ele);
        this.tagList = [];
        this.defaults = {
            version: 'version 1.0.0',
            initTags: [],
            editEnable: false,
            sortEnable: true,
            deleteBtn: false
        };
        this.options = $.extend({}, this.defaults, options);
    };
    bcTag.prototype = {
        render: function () {
            this._init();
            return this;
        },
        _init: function () {
            var self = this, p = this.options;
            self.element.hide();
            var ed = $('<ul class="tag-editor"></ul>').insertAfter(self.element);
            self.element.data('options', p); // set data on hidden field
            self.tagWrap = ed;
            self._initData();
            if (p.sortEnable) {
                self.tagWrap.sortable({
                    distance: 5, cancel: '.tag-editor-spacer, input',
                    update: function () {
                        self._update_globals();
                    }
                });
            }
        },
        _initData: function () {
            var self = this, p = this.options;
            if (p.initTags && p.initTags.length > 0) {
                self.tagList = p.initTags.slice(0);
                $.each(p.initTags, function () {
                    self.addTag(this);

                });
            }
        }
        ,
        addTag: function (item) {
            var self=this;
            self.tagList.push(item);
            var liEl = $("<li></li>");
            liEl.attr("val",item.value);
            liEl.data('data', item);
            liEl.append('<div class="tag-editor-spacer">&nbsp;</div><div class="tag-editor-tag">' + item.text + '</div>');
            self.tagWrap.append(liEl);
        } ,
        removeTag: function (val) {
             $("li[val='"+val+"']",self.tagWrap).remove();
            this._update_globals();
        },
        _update_globals:function () {
            var self=this;
            self.tagList = [];
            $('li',  self.tagWrap).each(function () {
                self.tagList.push($(this).data('data'))
            });
        },
        getTagList:function () {
            return this.tagList;
        }


    };
    $.fn.bcTag = function (options) {
        var bcTagArr = [];
        $(this).each(function () {
            var bcTagEle = new bcTag(this, options);
            bcTagArr.push(bcTagEle.render());
        });
        if (bcTagArr.length == 1) {
            return bcTagArr[0];
        }
        else {
            return bcTagArr;
        }
    };
})(jQuery);